import { ref, unref } from "vue";
import { saveTasksListLocalstory, clearTasksLocalstory } from "../utils/index";
import myWechart from "../images/wechart.jpg";
import gongzhonghao from "../images/wechart.jpg";
import wechart from "../images/wechart.jpg";
import zhifubao from "../images/zhifubao.png";

export function useFooter(tasks = [], setTasks) {
  const authorDialog = ref(null);
  const clearDialog = ref(null);
  const isShowFooter = ref(false);
  const clearDisabled = ref(true);
  // 是否禁用清除
  const setClearDisbled = (value) => {
    clearDisabled.value = value;
  };
  // 源码资源
  const handleSourceCode = () => {
    window.open("https://gitee.com/cpranmo/chrome-plugin-todo-list");
  };
  // 联系作者
  const handleContactAuthor = () => {
    isShowFooter.value = false;
    unref(authorDialog)?.open(["公众号", "微信"], [gongzhonghao, myWechart]);
  };
  // 打赏作者
  const handleRewardAuthor = () => {
    isShowFooter.value = true;
    unref(authorDialog)?.open(["微信", "支付宝"], [wechart, zhifubao]);
  };
  // 清除任务
  const handleClear = () => {
    unref(clearDialog)?.open();
  };
  // 确定清除
  const handleConfirmClear = (onlyDeleteDone) => {
    let newTasks = [];
    // 是否仅清除完成任务
    if (onlyDeleteDone) {
      newTasks = unref(tasks).filter((el) => !el.checked);
      saveTasksListLocalstory(newTasks);
    } else {
      newTasks = [];
      clearTasksLocalstory();
    }
    typeof setTasks === "function" && setTasks(newTasks);
  };
  return {
    authorDialog,
    clearDialog,
    isShowFooter,
    clearDisabled,
    setClearDisbled,
    handleSourceCode,
    handleContactAuthor,
    handleRewardAuthor,
    handleClear,
    handleConfirmClear,
  };
}
